package Top200;

/**
 * @author zhangmin
 * @create 2022-02-17 14:14
 */
public class myAtoi08 {
    public int myAtoi(String s) {
        int n = s.length();
        int index=0;
        int res=0;
        int flag=1;
        while (index< n &&s.charAt(index)==' '){
            index++;
        }
        if (index>=n) return 0;

        if (s.charAt(index)=='-'){
            flag=-1;
            index++;
        }else if (s.charAt(index)=='+'){
            index++;
        }

        while (index< n){
            int cur=s.charAt(index)-'0';
            if (cur>9||cur<0) break;;
            int t=res*10+cur;
            if (t/10!=res){
                return flag==1?Integer.MAX_VALUE:Integer.MIN_VALUE;
            }
            res=t;
            index++;
        }
        if (flag==1){
            return res;
        }else {
            return -1*res;
        }
    }

    public static void main(String[] args) {
        String s="2147483648";
        myAtoi08 obj=new myAtoi08();
        System.out.println(obj.myAtoi(s));
    }
}
